home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tch_tpas.zip / TL16.TXT < prev    next >
Text File  |  1986-04-05  |  6KB  |  214 lines

  1. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 74
  2.  
  3.  
  4. TURBO-LESSON 16:   REAL NUMBERS
  5.  
  6. OBJECTIVES - In this lesson, you will learn about:
  7.  
  8. 1.  Range of Real Numbers
  9. 2.  Input/Output of Real Numbers
  10. 3.  Calculations with Real Numbers
  11. 4.  Calculations with Integers and Real Numbers
  12.  
  13.  
  14. 1.  Range of Real Numbers
  15.  
  16. For business processing (dollar amounts), and scientific 
  17. processing, integers alone are not adequate.  Decimal numbers and 
  18. sometimes numbers in scientific notation are needed.
  19.  
  20. TURBO provides Real Numbers with 11 significant digits of 
  21. precision in the range:
  22.  
  23.     1E-38 to 1E+38
  24.  
  25. (The BCD version of TURBO provides 18 significant digits and a 
  26. range of  1E-63 to 1E+63, but this set of TURBO-LESSONS deals 
  27. with the more limited range above.)
  28.  
  29. ##### DO:
  30.  
  31. Run PROG16.
  32.  
  33. Enter 444.333222111 and examine the result presented in 
  34. scientific notation.
  35.  
  36. How many digits are retained before the E?  (I counted 11, 
  37. including digits on both sides of the decimal point.)
  38.  
  39. This is the 11 significant digits of precision.  Note that the 
  40. last 1 you entered was dropped.
  41. î
  42. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 75
  43.  
  44.  
  45. 2.  Input/Output of Real Numbers.
  46.  
  47. You will need to know what to expect with various combinations of 
  48. input and output of real numbers.
  49.  
  50. ##### DO:
  51.  
  52. Run PROG16.  
  53.  
  54. Enter 444.333222111 and study the various outputs.
  55.  
  56. When the real number, A, is output without any formatting, 
  57. scientific notation is used:
  58.  
  59. 4.4433322211 are the significant digits.
  60.  
  61. E+02   means multiply by 10 to the 2nd power to get the number.
  62.  
  63. If you want the number presented in some other form, you can add 
  64. the :w:d formatting to the name of the variable to print.
  65.  
  66. :w   Width of the print field
  67. :d   Decimal positions to print
  68.  
  69. WriteLn(A:10:2);  This statement would print the number, A, in a 
  70. print field 10 characters wide, with 2 decimal positions.
  71.  
  72. Look at the outputs on the screen again.  The formats used are 
  73. printed at the left.  Square brackets are printed to show the 
  74. width of the field and where the numbere is printed (left or 
  75. right-justified).
  76.  
  77. :-w   Width of print field, left-justify the number.
  78.  
  79. Notice the use of the minus sign to force printing of the number 
  80. at the left of the field.
  81.  
  82. What happens to the print field width when numbers are printed 
  83. left-justified?
  84.  
  85. ##### DO:
  86.  
  87. Run PROG16 with 3.4567 as input.
  88.  
  89. What happens when a print format is specified which will not hold 
  90. all of the significant digits?   Is the number rounded?
  91. î
  92. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 76
  93.  
  94.  
  95. 3.  Calculations with Real Numbers.
  96.  
  97. ##### DO:
  98.  
  99. In PROG16 change the ReadLn(A) to ReadLn(B).
  100.  
  101. Add after ReadLn(B):
  102.  
  103. A := B + C;
  104.  
  105. The result printed will be the sum of B, which you enter, and C 
  106. which is a constant, -2.0.
  107.  
  108. Run the program with 2.34 as input.
  109.  
  110. Are the results as expected?
  111.  
  112. In scientific calculations, very large, and very small numbers 
  113. are sometimes needed.  Can you enter these in a convenient form 
  114. without a long string of zeros?
  115.  
  116. The radius of the earth is 6370000 meters.
  117. This is the same as        6.37 times 100000
  118. Which is the same as       6.37 times 10 to the 6th power
  119. Which may be entered as    6.37E6 or 6.37E06 
  120.                         or 6.37E+6 or 6.37E+06
  121.  
  122. ##### DO:
  123.  
  124. Run the program with 6.37E6 as input.
  125.  
  126.  
  127.  
  128. Try the other 3 forms listed above.
  129.  
  130. Are the results the same in all cases?
  131. î
  132. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 77
  133.  
  134.  
  135. ##### DO:
  136.  
  137. Run the program with 6370000 as input.
  138.  
  139. Does it make any difference whether the number is input in 
  140. scientific notation or in the usual form? 
  141.  
  142. ##### DO:
  143.  
  144. Run the program with 6.37E7 as input.
  145.  
  146. What happens when the result is too large for the format?
  147. (Count the character positions used to print A:10:2).
  148.  
  149. ##### DO:
  150.  
  151. Get a new copy of the original PROG16.
  152.  
  153. Run the program with 6.37E-6 as input.
  154.  
  155. Notice the unformatted output is correct, but the formatted 
  156. output shows nothing but zeros.
  157.  
  158. The result, A, is 0.00000637, with significant digits too far to 
  159. the right to show up in the formatted output.
  160.  
  161. ##### DO:
  162.  
  163. Change the WriteLn format, A:10:2 to A:10:6.  (Notice there are 
  164. two A:10:2's in the statement).  
  165.  
  166. Run the program with the following values:
  167.  
  168. 6.37E6,  6.37E-6,  6.37E-4
  169. î
  170. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 78
  171.  
  172.  
  173. 4.  Calculations with Integers and Real Numbers.
  174.  
  175. What happens when you mix Integers and Real numbers in 
  176. calculations?
  177.  
  178. ##### DO:
  179.  
  180. Get a copy of the original PROG16 and make the following changes:
  181.  
  182. Change ReadLn(A); to ReadLn(I);
  183.  
  184. Change the prompt to Write('Enter an Integer: ');
  185.  
  186. Add after the ReadLn statement:
  187.  
  188. J := I + C;
  189.  
  190. Run the program.
  191.  
  192. What results did you get?
  193.  
  194. The "Type mismatch" refers to J.  
  195.  
  196. Since the calculation involves both an integer, I, and a real 
  197. number, C, the result cannot be stored in an integer variable.  
  198. If the result had significant digits after the decimal, they 
  199. would be lost when stored as an integer.  
  200.  
  201. ##### DO:
  202.  
  203. Change the calculation to:
  204.  
  205. A := I + C;
  206.  
  207. Run the program.
  208.  
  209.  
  210. NOTE: ON YOUR OWN, YOU MAY WANT TO EXPERIMENT WITH THE WRITELN 
  211.       FORMATS.  CHANGE THE FORMATS TO VARIOUS VALUES AND TRY THEM 
  212.       WITH A VARIETY OF INPUTS.  
  213. î
  214.